package org.datacoder.geocoding.datastructures.points

import org.datacoder.geocoding.datastructures.geoshapes.*
import org.datacoder.geocoding.jurisdiction.*

class PairedSortedArrays implements PointsWithinRectangleFinder {

    SortedArray jurisdictionsByLon
    SortedArray jurisdictionsByLat

    public PairedSortedArrays(Collection<Jurisdiction> jurisdictions){
        jurisdictionsByLon = new SortedArray(jurisdictions, GeoShape.LON)
        jurisdictionsByLat = new SortedArray(jurisdictions, GeoShape.LAT)
    }

    public Set<Jurisdiction> findPointsWithinRectangle(GeoRectangle gr){
        Set<Jurisdiction> lonJurisdictions = jurisdictionsByLon.findPointsWithinInterval(gr.toGeoInterval(GeoShape.LON))
        Set<Jurisdiction> latJurisdictions = jurisdictionsByLat.findPointsWithinInterval(gr.toGeoInterval(GeoShape.LAT))
        lonJurisdictions.retainAll(latJurisdictions)
        return lonJurisdictions
    }

}

